package com.shangcheng.server.controller;

import com.shangcheng.model.domain.SmsHomeAdvertise;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.SmsHomeAdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {

    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 添加广告
     *
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("create")
    public Result create(@RequestBody SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseService.create(smsHomeAdvertise);
        return new Result(200, null, "添加成功");
    }

    /**
     * 删除广告
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public Result delete(List<Integer> ids) {
        smsHomeAdvertiseService.delete(ids);
        return new Result(200, null, "删除成功");
    }

    /**
     * 分页查询广告
     *
     * @param endTime
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("list")
    public Result findAllSmsHomeAdvertiselist(@RequestParam(required = false) String endTime,
                                              @RequestParam(required = false) String name,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                              @RequestParam(required = false) Integer type) {
        PageResult pr = smsHomeAdvertiseService.findList(endTime, name, pageNum, pageSize, type);
        return new Result(200, pr, "");
    }

    /**
     * 修改上下线状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("update/status/{id}")
    public Result updateStatus(@PathVariable("id") Integer id, Integer status) {
        smsHomeAdvertiseService.updateStatus(id, status);
        return new Result(200, null, "修改成功");
    }

    /**
     * 修改上下线状态
     *
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("update/{id}")
    public Result update(@RequestBody SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseService.update(smsHomeAdvertise);
        return new Result(200, null, "修改成功");
    }

    /**
     * 获取广告详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result update(@PathVariable("id") Integer id) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseService.findById(id);
        return new Result(200, smsHomeAdvertise, "");
    }
}
